package com.lizx.persional.resource;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.lizx.persional.common.utils.Result;
import com.lizx.persional.common.utils.ResultPage;
import com.lizx.persional.entity.Department;
import com.lizx.persional.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  api
 * </p>
 *
 * @author 李振兴
 * @since 2018-02-27
 */
@RestController
@RequestMapping("/persional/department")
public class DepartmentResource {
    @Autowired
    private DepartmentService departmentService;

    /**
     * 部门列表
     * @param page
     * @param department
     * @return
     */
    @GetMapping("/page")
    public ResultPage page(Page<Department> page, Department department){
        EntityWrapper<Department> ew = new EntityWrapper<>(department);
        ew.orderBy("sort");
        return ResultPage.success(departmentService.selectPage(page,ew));
    }

    /**
     * 新增
     * @param department
     * @return
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody Department department) {
        return Result.success(departmentService.insert(department));
    }

    /**
     * 根据id获取
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public Result getDepartment(@PathVariable Long id){
        return Result.success(departmentService.selectById(id));
    }

    /**
     * 修改
     * @param department
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody Department department){
        return Result.success(department.updateById());
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @DeleteMapping("/delete")
    public Result delete(@RequestBody List<Long> ids) {
        return Result.success(departmentService.deleteBatchIds(ids));
    }

    /**
     * 部门下拉菜单
     * @param department
     * @return
     */
    @GetMapping("/list")
    public Result list( Department department){
        EntityWrapper<Department> ew = new EntityWrapper<>(department);
        ew.eq("is_enable",true);
        return Result.success(departmentService.selectList(ew));
    }
}

